package com.bw.gmall.api.repository;

import com.bw.gmall.api.model.PageTrafficStats;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PageTrafficStatsRepository {
/*
* PageTrafficStatsRepository：页面流量统计数据访问类，
* 提供查询页面流量 TopN 和时间范围内数据的方法，操作表dws_page_traffic_topn_window。
* */
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<PageTrafficStats> findTopN(int limit) {
        String sql = "SELECT * FROM dws_page_traffic_topn_window ORDER BY rank ASC LIMIT " + limit;
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PageTrafficStats.class));
    }

    public List<PageTrafficStats> findByTimeRange(String startTime, String endTime) {
        String sql = "SELECT * FROM dws_page_traffic_topn_window WHERE stats_time BETWEEN ? AND ? ORDER BY rank ASC";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PageTrafficStats.class), startTime, endTime);
    }
}